package rd.hardcore.ui.pad.stream 
{
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import fr.digitas.flowearth.conf.Conf;
	import rd.hardcore.datas.Track;
	import rd.hardcore.engine.datas.DatasManager;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardEvent;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.engine.tools.Tools;
	import rd.hardcore.events.SaveManagerEvent;
	import rd.hardcore.ui.pad.PadsManager;
	import rd.hardcore.ui.pad.stream.StreamPad;
	/**
	 * ...
	 * @author Romain Prache
	 */
	public class StreamPadsManager extends PadsManager
	{
		
		private var l	:Loader;
		
		public function StreamPadsManager( _id : int) 
		{
			super(_id);
		}
		
		/* BUILD */
		override public function build(pads:XML):void 
		{			
			// Get Pads
			for each( var node : XML in pads.padmanager.(@id == ID).children() )
			{
				var pad	: StreamPad = new StreamPad();
				pad.build(node);
				vecPads.push(pad);
			}
							
			displayGrid();
			
			if ( StreamBoardManager.instance.STATE == StreamBoardManager.STREAM_BOARD_READY )
				loadBG();
			else
				StreamBoardManager.instance.addEventListener( StreamBoardEvent.BOARD_READY_TO_DISPLAY, loadBG );
		}
		
		/* DISPOSE */
		override public function dispose() : void
		{
			super.dispose();
			
			if ( l )
			{
				l.unload();
				cleanListeners();
				l = null;
			}
		}
		
		/* LAYOUT */
		override protected function displayGrid(e:Event = null):void 
		{
			super.displayGrid(e);
		}
		
		private function loadBG ( evt : Event = null ) : void
		{
			l = new Loader();
			l.mouseChildren = false;
			l.mouseEnabled = false;
			l.contentLoaderInfo.addEventListener( Event.COMPLETE, success);
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, fail);
			l.load( new URLRequest( Tools.macOsUrlProofing(File.applicationStorageDirectory.resolvePath(StreamBoardManager.instance.getBackground(ID) ).nativePath)));
			addChildAt(l, 0);
			
			var msk : Shape = new Shape();
			msk.graphics.beginFill(0);
			msk.graphics.drawRoundRectComplex(0, 0, 760, 253, 0, 0, 13, 13);
			msk.graphics.endFill();
			addChild(msk);
			
			l.y = msk.y = -5;
			
			l.mask = msk;
		}
		
		private function success(e:Event):void 
		{
			cleanListeners();
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			cleanListeners();
		}
		
		private function cleanListeners() : void
		{
			l.contentLoaderInfo.removeEventListener( Event.COMPLETE, success);
			l.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, fail);
		}
		
		
		
	}

}